/**
 * @filename:MilvusConfigServiceImpl 2025-04-15
 * @project starhub-schema  V1.0
 */
package com.starhub.application.rag.service.impl;

import com.starhub.application.rag.mapper.MilvusConfigMapper;
import com.starhub.application.rag.service.MilvusConfigService;
import com.starhub.common.bean.model.MilvusConfig;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.engine.generator.genid.IdGenerator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**   
 * @Description:(向量库配置服务实现)
 *
 * @version: V1.0
 * @author: luosw
 * 
 */
@Service
public class MilvusConfigServiceImpl implements MilvusConfigService {

    @Autowired
    private MilvusConfigMapper milvusDao;

    /**
    * 新增
    * @param user
    * @param milvus
    * @return
    */
    public Long add(LoginUser user, MilvusConfig milvus){
        Long id = IdGenerator.generateId();
        //数据封装，默认值添加
        milvus.setId(id);
        milvus.setCreateTime(new Date());
        milvus.setUpdateTime(new Date());
        milvus.setDeleteType(0);
        milvusDao.insertSelective(milvus);
        return id;
    }

    /**
    * 更新
    * @param user
    * @param milvus
    * @return
    */
    public MilvusConfig update(LoginUser user, MilvusConfig milvus){
        milvus.setUpdateTime(new Date());
        //不允许更新的属性设置为null
        milvusDao.updateByPrimaryKeySelective(milvus);
        return milvus;
    }

    /**
    * 基本信息获取
    * @param user
    * @param id
    * @return
    */
    public MilvusConfig getInfo(LoginUser user, Long id){
        MilvusConfig milvus = milvusDao.selectByPrimaryKey(id);
        return milvus;
    }

    /**
    * 根据mark获取配置信息
    * @param mark 标记
    * @return Milvus配置信息
    */
    @Override
    public MilvusConfig getConfigByMark(String mark) {
        return milvusDao.selectByMark(mark);
    }

    /**
    * (批量)删除，同时支持单条或者批量,此方法为逻辑删除
    * @param user
    * @param ids
    */
    public void deleteByType(LoginUser user, String ids,int deleteType){
        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        //删除状态，目前只有1的逻辑
        milvusDao.deleteMilvusConfigByType(idList, deleteType);
    }

    /**
    * 根据id删除，此方法为真实删除
    * @param user
    * @param ids
    */
    public void deleteRecord(LoginUser user, String ids){
        //数组转换
        List<Long> idList = Arrays.stream(ids.split(","))
            .map((id) -> Long.parseLong(id))
            .collect(Collectors.toList());
        milvusDao.deleteMilvusConfigRecord(idList);
    }

    /**
    * 列表查询
    * @param user
    * @param params
    */
    public List<MilvusConfig> getList(LoginUser user,  Map<String , Object>  params){
        List<MilvusConfig> milvusList =  new ArrayList<MilvusConfig>();
        return milvusList;
    }

    @Override
    public List<MilvusConfig> getAllConfigs() {
        List<MilvusConfig> milvusList =  new ArrayList<MilvusConfig>();
        return milvusList;
    }
}